#include"groupmodel.hpp"
#include"CommonConnectionPool.hpp"

#include"db.h"

//创建组
bool GroupModel::createGroup(Group &group)
{
    // 1. 组装sql语句
    char sql[1024] ={0};
    sprintf(sql,"insert into allgroup(groupname, groupdesc) values('%s','%s')",
    group.getName().c_str(),group.getDesc().c_str());

   shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
    if(mysql->update(sql))
    {
        group.setId(mysql_insert_id(mysql->getConnection()));
        return true;
    }  
    return false;
}

//加入群组
void GroupModel::addGroup(int userid,int groupid,string role)
{
    // 1. 组装sql语句
    char sql[1024] ={0};
    sprintf(sql,"insert into groupuser values(%d,%d,'%s')",
        groupid,userid,role.c_str());

     shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
     mysql->update(sql);      
}

//查询用户所在群组的消息
vector<Group> GroupModel::queryGroups(int userid)
{
    // 1. 组装sql语句
    char sql[1024] ={0};
    sprintf(sql,"select a.id,a.groupname,a.groupdesc from allgroup a join \
    groupuser b on a.id = b.groupid where b.userid=%d",userid);

    vector<Group> groupVec;
    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
       

    //拿到该用户组的 信息
    MYSQL_RES * res = mysql->query(sql);
    if(res != nullptr)
    {
        MYSQL_ROW row;
        //把userid 用户的所有离线消息 都放入 vec中
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            Group group;
            group.setId(atoi(row[0]));
            group.setName(row[1]);
            group.setDesc(row[2]);

            groupVec.push_back(group);
        }//不断得拿

        //释放资源
        mysql_free_result(res);  
    }

    //拿到每个组 用户的信息
    for(Group &group : groupVec)
    {
    char sql[1024] ={0};
    sprintf(sql,"select a.id,a.name,a.state,b.grouprole from user a \
    inner join groupuser b on b.userid = a.id where b.groupid=%d",
    group.getId());

    MYSQL_RES * res = mysql->query(sql);
    if(res != nullptr)
    {
        MYSQL_ROW row;

        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            GroupUser user;
            user.setId(atoi(row[0]));
            user.setName(row[1]);
            user.setState(row[2]);
            user.setRole(row[3]);

            group.getUsers().push_back(user);
        }//不断得拿

        //释放资源
        mysql_free_result(res);  
    }         
    }
    return groupVec;
}

// 根据指定的 groupid 查询群组用户id列表
vector<int> GroupModel::queryGroupUsers(int userid,int groupid)
{
    char sql[1024] ={0};
    sprintf(sql,
    "select userid from groupuser where groupid = %d and userid != %d",groupid,userid); 

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getConnection();
      
    vector<int> idVec;
    MYSQL_RES * res = mysql->query(sql);
    if(res != nullptr)
    {
        MYSQL_ROW row;
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            idVec.push_back(atoi(row[0]));
        }//不断得拿

        //释放资源
        mysql_free_result(res);  
    }
    return idVec;  
}